package UIComponents
{
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	
	public class UpgradePanel extends Sprite
	{
		public var upbtn:MyButton;
		public var collectIcon:Sprite;
		public function UpgradePanel()
		{
			init();
		}
		private function init():void
		{
			collectIcon=this.getChildByName("upAfterIcon") as Sprite;
			this.collectIcon.mouseChildren=false;
			this.upbtn=this.getChildByName("upLvBtn") as MyButton;
			addLister();
		}
		
		private function addLister():void
		{
			this.upbtn.addEventListener(ComponentEvent.BUTTON_DOWN,upFun);
		}
		
		private function upFun(e:ComponentEvent):void
		{
			this.imission.finishHandler(true);
		}
		private var imission:IMission; 
		public function setUpData(arr:Array,obj:Object):void
		{
			if(null==arr || arr.length<1)return;
			var btnFlag:Boolean=true;
			this.imission=arr[0] as IMission;
			this.imission.missionShowIcon.scaleX=1.0;
			this.imission.missionShowIcon.scaleY=1.0;
			this.reset();
//			this.setCollectIcon(imission.missionShowIcon);
			
			//跟设计后的建筑加热感
			var tips:String=this.imission.missionName+"："+obj.nextDes;
			this.collectIcon.addChild(imission.missionShowIcon);
			ToolTip_Sprite.getInstance().setTip(collectIcon,new MyTipSprite(tips),ToolTip_Sprite.DIRECTION_LEFT);
			
			//给前面的建筑加热感
			(this.getChildByName("upBefroeIcon") as IconContainer).addPic(obj.buildIcon,obj.name+"："+obj.description);
			
			//控制星星 BLV
			var cLv:int=int(obj.BLV);
			for(var j:int=1;j<=cLv;j++)
			{
				this.getChildByName("l"+j).filters=null;
				this.getChildByName("r"+j).filters=null;
			}
			this.getChildByName("r"+(cLv+1)).filters=null;
			
			var conditionCountsAry:Array=imission.conditionCountsAry;
			for(var i:int=0;i<6;i++)
			{
				var frameCollectSuit:UpgradeItemPanel=this.getFrameCollectByIndex(i);
				frameCollectSuit.upmb.visible=true;
				frameCollectSuit.reset();
				var obj:Object=conditionCountsAry[i];
				if(null!=obj)
				{
					var tmpFlag:Boolean=frameCollectSuit.setUpItemData(obj);
					if(!tmpFlag)
					{
						btnFlag=tmpFlag;
					}
				}	
			}
			if(!btnFlag)
			{
				this.upbtn.enabled=false;
				this.upbtn.filters=this.getFilter();
			}else{
				this.upbtn.enabled=true;
				this.upbtn.filters=null;
			}
			
		}
		
		
		private function setCollectIcon(value:DisplayObject):void
		{
			var tips:String=this.imission.missionName+"："+this.imission.missionDescription;
			this.collectIcon.addChild(value);
			ToolTip_Sprite.getInstance().setTip(collectIcon,new MyTipSprite(tips),ToolTip_Sprite.DIRECTION_LEFT);
		}
		
		private function reset():void
		{
			while(this.collectIcon.numChildren>0) this.collectIcon.removeChildAt(0);
			for(var i:int=1;i<=5;i++)
			{
				this.getChildByName("l"+i).filters=getFilter();
				this.getChildByName("r"+i).filters=getFilter();
			}
		}
		
		private function getFilter():Array
		{
			var matrix:Array = new Array();
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // red
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // green
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // blue
			
			matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
			
			var gray:ColorMatrixFilter = new ColorMatrixFilter(matrix);
			
			var filtersArray:Array = new Array(gray);
			return filtersArray;
		}
		
		private function getFrameCollectByIndex(index:int):UpgradeItemPanel
		{
			if(index<0 || index>5)throw new Error("索引出错");
			return (this.getChildByName("item"+(index+1))) as UpgradeItemPanel;
		}
	}
}